template.defaults.imports.CountdownTime = function (factEndDate) {
    var timestamp = (new Date()).valueOf();
    return factEndDate - timestamp;
};

template.defaults.imports.timestampTransfor = function (timestamp) {
    var d = new Date(timestamp); //根据时间戳生成的时间对象
    var date = (d.getFullYear()) + "-" + 
    (d.getMonth() + 1) + "-" +
    (d.getDate()) + " " + 
    (d.getHours()) + ":" + 
    (d.getMinutes()) + '' ;
    return date;
};

$(function () {
    //******************* start 排序条件及其筛选条件框 ***********************/
    $('.weui-flex__item').click(function () {
        $('.weui-flex__item').removeClass('current');
        $(this).addClass('current');
        var display = $(this).find('.children').css('display');
        if (display == 'none') {
            $(this).find('.children').show();
        } else {
            $(this).find('.children').hide();
        }

        var type = parseInt($(this).attr('orderType'));
        if (type){
            orderType = type;
            queryList(function(){},false);
        }
    });

    $('.weui-flex__item .children .item').click(function () {
        $('.weui-flex__item .children .item').removeClass('current');
        $(this).addClass('current');
        var selectName = $(this).find('text').text();
        var _parent = $(this).parent().parent();
        _parent.addClass('current');
        _parent.find('.placeholder text').text(selectName);
        orderType = parseInt($(this).attr('orderType'));
        queryList(function(){});
    });

    $('.mask').click(function () {
        closeAll();
    });
    $('#screen').click(function () {
        $('.mask').show();
        $('.screen_condition').show().animate({
            right: '0'
        });
    });

    $(window.document.body).click(function (e) {
        var target = $(e.target);
        var isHide = true;
        while (target.prop("tagName")) {
            if (target.hasClass('weui-flex__item')) {
                isHide = false;
                break;
            } else {
                target = target.parent();
            }
        }
        if (isHide)
            $('.weui-flex__item .children').hide();
    }); 

    //带条件和分类查询
    $('#screen_condition_confirm').click(function(){
        keyword = $('.screen_condition .item .list input[name="keyword"]').val();
        cate1 = $('.screen_condition .item .list .current').attr('value');
        queryList();
        closeAll();
    });
    $('#screen_condition_reset').click(function(){
        resetCondition();
    });

    function closeAll(){
        $('.mask').hide();
        $('.weui-flex__item .children').hide();
        $('.screen_condition').css({
            right: '-6.2rem'
        }).hide();
    }
    //******************* end 排序条件及其筛选条件框 ***********************/

    var loading = false; //状态标记
    var hasNextPage = false;
    var pageNo = 1;
    var orderType = 1;
    var cate1 = 0;
    var keyword = false;
    queryList();

    //下拉刷新
    $("#weipai").pullToRefresh().on("pull-to-refresh", function () {
        $("#weipai-list-content .left-list").html("");
        $("#weipai-list-content .right-list").html("");
        pageNo = 1;
        queryList(function(){
            $("#weipai").pullToRefreshDone(); // 重置下拉刷新
        });
    });

    //上拉加载更多
    $('#weipai').infinite().on("infinite", function () {
        if (hasNextPage){
            pageNo++;
            queryList(function(){},true);
        }        
    });

    //复位所有参数
    function resetCondition(){
        pageNo = 1;
        orderType = 1;
        cate1 = 0;
        keyword = false;
        $('.screen_condition .item .list .unit').removeClass('current');
        $('.screen_condition .item .list input[name="keyword"]').val('');
    }

    function queryList(callback,isMore){
        if (loading) return;
        loading = true;
        $.post('/antique/api/mallProduct/web/query_list', {
            start: (pageNo - 1) * 20,
            limit: 20,
            name:keyword ? keyword : '',
            orderType:orderType,
            firstCategorys:cate1 ? cate1 : '',
            auctionId: id
        }, function (result) {
            if (!isMore){
                var leftList = [];
                var rightList = [];
                if(result.data.list){
                    for(var i = 0;i<result.data.list.length;i++){
                        if(i%2 == 0){
                            leftList.push(result.data.list[i]);
                        } else {                        
                            rightList.push(result.data.list[i]);
                        }
                    }
                }
                /* global template */
                document.getElementById('left-list-content').innerHTML = template('left-list', {list:leftList});
                document.getElementById('right-list-content').innerHTML = template('right-list', {list:rightList});            
            } else {
                $("#weipai-list-content").append(template('weipai-list', result.data));
            }

            $('.weipai-list .lot .info .price .time time[sign="unmake"]').each(function () {
                $(this).removeAttr('sign');
                /* global Countdown */
                Countdown($(this).attr('time'), $(this));
            });
            var zcHeight = $('.zc').height();
            $('.weui-flex').css({
                top:zcHeight
            });
            hasNextPage = result.data.hasNextPage;
            if (!result.data.hasNextPage) {
                $('#loadmore').hide();
                $('#nomore').show();
            } else {
                $('#loadmore').show();
                $('#nomore').hide();
            }
            if (callback) callback();
            loading = false;
        });
    }

    /* global id */
    $.post('/antique/api/mallAuction/web/query', {
        id: id
    }, function (result) {
        document.getElementById('zc-content').innerHTML = template('zc', result);   
    });
});